/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : my_info_pw.js                            
*  Description  : 마이홈 비밀번호 입력
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-05-12  김효순    Created.  
*  2012-05-12  김효순    Complete.
*******************************************************************************/
jQuery(function($){
	// 비밀번호
	function clsPasswd()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOVALUE": "비밀번호를 입력해 주십시요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsPasswd.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content input:text");
			$layer.after("<input type='password' />").next("input:password").attr({
				"class": $layer.attr("class")
				, "style": $layer.attr("style")
			}).end().remove();
			$layer = $("div#container section#main_content input:password");
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, isValid: function(){
			this.errMsg = null;
			if(this.getValue() == ""){
				this.errMsg = "_NOVALUE";
				return false;
			}
			return true;
		}
		, getError: function(){
			if(this.errMsg){
				return this._ERRMSG[this.errMsg];
			}else{
				return null;
			}
		}
		, getValue: function(){
			return this._HTMLDom["$layer"].val();
		}
		, focus: function(){
			this._HTMLDom["$layer"].trigger("focus");
		}
	}
	
	// 서버통신
	function clsComm()
	{
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){

		}
		, authSubmit: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "authPw";
			var options = {
				"pw": arg["비밀번호"]
			}
			_gf.ajax({
				"sid": "US001"
				, "servicename": serviceName
			}, options, function(ret){
				document.objControl.finishAuthSubmit({
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": document.objGlobal.getResultMsg(ret)
				});
			});
			/*setTimeout(function(){
				var rnd = document.objGlobal.random(0, 1);
				
				var ret = {
					"처리결과": rnd
					, "메시지": null
				}
				
				document.objControl.finishAuthSubmit(ret);
			}, 1000);*/
		}
	}
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objPasswd = null;
		
		this._permitStart = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objPasswd = new clsPasswd();
			
			this._beforeStart();
			this._bind();
			
		}
		, _preload: function(){
			_gf.spinStart();
			
			_util.delCookie("check_auth", "/");
		}
		, _beforeStart: function(){
			var thisClass = this;
			
			this._permitStart = {
				
			}
			
			
			$.ajax({
				url: _ROOT + "/js/mypage_common.js"
				, dataType: "script"
				, async: false
				, success: function(){
					
				}
				, error: function(){
					alert("error");
				}
			});
			
			_util.executeThread(function(){
				for(key in thisClass._permitStart){
					if(!thisClass._permitStart[key]) return false;
				}
				
				return (_gf.getMyInfoData());
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
		}
		, _bind: function(){
			var thisClass = this;
			
			if($.browser.msie && ($.browser.version.substrEx(0, 1)-0) < 9){
				this.objPasswd.$getLayer().keydown(function(e){
					e = e || event;
					
					if(e.keyCode == 13) thisClass.authSubmit();
				});
			}
			
			// 확인클릭
			$("div#container").on("click", "input:image[alt='확인']", function(){
				thisClass.authSubmit();
			});
		}
		, _start: function(){
			
		}
		// 인증완료
		, finishAuthSubmit: function(ret){
			_gf.spinEnd();
			
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000"){
				this.objPasswd.focus();
				return;
			}
			
			_util.setCookie("check_auth", "Y", null, "/");
			
			var url = document.referrer;
			if(!url) url = _ROOT + "/mypage/my_privacy.html";
			document.location.href = url;
		}
		// 인증전송
		, authSubmit: function(){
			if(!this.objPasswd.isValid()){
				alert(this.objPasswd.getError());
				this.objPasswd.focus();
				return;
			}
			
			_gf.spinStart("sending...");
			this.objComm.authSubmit({
				"비밀번호": this.objPasswd.getValue()
			});
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});
